package com.white.androidstudy.main;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.white.androidstudy.R;

/**
 * Created by ytf on 2017/11/5 005.
 * Description:
 */

public class LaunchAdapter extends RecyclerView.Adapter
{
    private String[] names;

    public LaunchAdapter(String[] names)
    {
        this.names = names;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        View v = View.inflate(parent.getContext(), R.layout.item_function, null);
        return new VH(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
    {
        VH vh = (VH) holder;
        vh.name.setText(names[vh.getAdapterPosition()]);
    }

    @Override
    public int getItemCount()
    {
        return names.length;
    }

    private class VH extends RecyclerView.ViewHolder
    {
        TextView name;

        VH(View itemView)
        {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.name);
            if(l != null)
            {
                name.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        int pos = getAdapterPosition();
                        l.onClick(pos, names[pos]);
                    }
                });
            }
        }
    }


    public void setOnItemClickListener(OnItemClickListener a)
    {
        l = a;
    }

    private OnItemClickListener l;

    interface OnItemClickListener
    {
        void onClick(int position, String item);
    }
}
